global input "Q:\dc1prhcmsas01\PU2\data - sas"
global temp "Q:\dc1prhcmsas01\PU2\temp_stata_wr"
global log "Q:\dc1prhcmsas01\PU2\Log - Stata WR"
global output "Q:\dc1prhcmsas01\PU2\data_stata_wr"
global graphs "Q:\dc1prhcmsas01\PU2\graphs_stata_wr"
global tables "Q:\dc1prhcmsas01\PU2\tables_stata_wr"

cd "Q:\dc1prhcmsas01\PU2\Code - Stata WR"
set scheme plotplain
graph set window fontface "Times New Roman"

capture log close
log using "$log/5_clean_staggered_bargaining", replace

*Program to collapse data by firm-bin and month of each worker's last pay increase
	cap program drop clean
	program define clean
	args ref year1 year2 treat

	*For each worker, identify the month that they received a wage increase in year before policy 
	use yr_month client_id state emp_pur_c salaried0 base using "$output\stayers_panel_`ref'.dta", clear
	keep if salaried0==1
	preserve 
		keep if yr_month>=mdy(5,1,`year1') & yr_month<=mdy(5,1,`year2')
		bysort client_id state emp_pur_c (yr_month): gen diff_base=base-base[_n-1]
		gen month=month(yr_month) if diff_base!=0 & diff_base!=.
		collapse (max) last_increase=month, by(client_id state emp_pur_c)
		drop if last_increase==.
		keep client_id state emp_pur_c last_increase
		tempfile temp 
		save `temp'
	restore 
	merge m:1 client_id state emp_pur_c using `temp'
	drop _merge 
	
	*Collapse 
	gen bin=floor((base-913)/40)*40+913
	keep if bin<=2500
	collapse (count) n=base , by(yr_month client_id last_increase bin)
	drop if last_increase==.
	
		*Expand to fill in bin
			gegen group=group(yr_month client_id last_increase)
			replace bin=(bin-913)/40
			tsset group bin  
			tsfill, full
				
			bysort group: carryforward yr_month, replace
			bysort group: carryforward client_id, replace
			bysort group: carryforward last_increase, replace
			
			gsort group -bin 
			bysort group: carryforward yr_month, replace
			bysort group: carryforward client_id, replace
			bysort group: carryforward last_increase, replace
				
			drop group
			replace bin=bin*40+913
			
		*Fill in missing outcomes 
			replace n=0 if n==.
			
		bysort yr_month client_id last_increase: egen emp=sum(n)
		keep if bin==913	
		gen share=n/emp
		gen treat=`treat'
		gen time=mofd(yr_month)-mofd(mdy(4,01,`year2'))
		save "$temp/temp_`ref'", replace 
	end 
		
	clean treat 2015 2016 1
	clean control 2013 2014 0
	
	use "$temp/temp_treat.dta", clear
	append using "$temp/temp_control.dta"
	save "$temp/staggered_bargaining.dta", replace 
	
	*Delete temp files
	erase "$temp/temp_treat.dta"
	erase "$temp/temp_control.dta"